package com.z_utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;

/*
   MyBatis 工具类

 */
public class MyBatisUtils {
    // 私有构造方法
    private MyBatisUtils() {
    }

    // 声明连接工厂对象
    private static SqlSessionFactory sqlSessionFactory;

    static {

        try {
            //加载核心配置文件
            sqlSessionFactory=
                    new  SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("MyBatisConfig.xml"));
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    // 提供静态方法，返回SqlSession对象

    public static SqlSession getSqlSession(){
        return sqlSessionFactory.openSession(true);
    }
}
